$(function () {
    //自定义正则
    const form = layui.form

    form.verify({
        nickname: [
            /^[\S]{1,10}$/,
            '昵称的长度为1-10的非空字符串'
        ]
    })


    //封装渲染数据的函数
    const layer = layui.layer
    initUserInfo()
    function initUserInfo() {
        axios({
            url: '/my/userinfo',
        }).then(res => {
            //成功回调
            if (res.data.message !== '获取用户基本信息成功！') {
                return layer.msg(res.data.message)
            }
            form.val('formUserInfo', res.data.data)
        })
    }

    //表单的重置
    $('#btnReset').on('click', function (e) {
        e.preventDefault()
        initUserInfo()
    })

    $('form').on('submit', function (e) {
        e.preventDefault()
        //提交按钮发送请求
        axios({
            url: '/my/userinfo',
            method: 'post',
            data: $('form').serialize()

        }).then(res => {
            //成功回调
            if(res.data.status != 0){
                return layui.msg(res.data.message)
            }
            window.parent.getUserInfo()

        })
    })


})